package com.backwaveweibo.bww.service.impl;

import com.backwaveweibo.bww.cache.ApplicationContextHolder;
import com.backwaveweibo.bww.entity.User;
import com.backwaveweibo.bww.service.FollowService;
import com.backwaveweibo.bww.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service
@Slf4j
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
public class FollowServiceImplSet implements FollowService {
    @Resource
    RedisUtil redisUtil;
    

    //爷的粉丝
    private static final String MY_FANS = "USER_%s:MY_FANS";
    //爷的关注
    private static final String MY_SUBSCRIBE = "USER_%s:MY_SUBSCRIBE";


    private RedisUtil getRedisUtil(){
        if (redisUtil == null){
            redisUtil = ApplicationContextHolder.getBean("redisUtil");
        }
        redisUtil.changeDb(0);
        return redisUtil;
    }

    /**
     *
     * @param fansId 传入id
     * @param user 传入用户
     * @解释 判断这个用户是否关注了这个id所代表的用户
     * @return
     */
    @Override
    public Boolean isFans(Long fansId, User user) {
        //格式化key
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_FANS, user.getUserId());
        boolean b = redisUtil.sHasKey(fanUsersKey, String.valueOf(fansId));
        return b;
    }

    @Override
    public Boolean isSubscribe(Long subscribeId, User user) {
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        Object value = redisUtil.sHasKey(fanUsersKey, String.valueOf(subscribeId));
        return value != null ;
    }

    /**
     * 关注功能
     * @param followId 传入要关注的人的id
     * @param user  操作主体
     * @return
     */
    @Override
    public Boolean setFollow(Long followId, User user){
        redisUtil = getRedisUtil();
        //给自己的关注增加一个人
        String usersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        long sSet1 =redisUtil.sSet(usersKey, String.valueOf(followId));

        //给我关注的人增加一个粉丝
        String fanUsersKey = String.format(MY_FANS, followId);
        long sSet = redisUtil.sSet(fanUsersKey, String.valueOf(user.getUserId()));
        return sSet == 1 && sSet1 == 1;

    }

    /**
     * 取消关注功能
     * @param unFollowId 这是我想要取消关注人的id
     * @param user  这是爷
     */
    @Override
    public Boolean setUnFollow(Long unFollowId, User user){
        redisUtil = getRedisUtil();

        //给自己的关注减少一个人
        String followUsersKey = String.format(MY_SUBSCRIBE, user.getUserId());
        long setRemove = redisUtil.setRemove(followUsersKey, String.valueOf(unFollowId));

        //给我关注的人减少一个粉丝
        String fanUsersKey = String.format(MY_FANS, unFollowId);
        long setRemove1 = redisUtil.setRemove(fanUsersKey, String.valueOf(user.getUserId()));
        return setRemove == 1 && setRemove1 == 1;

    }

    public Boolean delUserKey(Long id){
        log.info("删除用户粉丝和订阅表");
        redisUtil = getRedisUtil();
        String subscribe = String.format(MY_SUBSCRIBE, id);
        String fans = String.format(MY_FANS,id);
        redisUtil.del(subscribe,fans);
        return true;
    }

    /**
     * 获取爷的关注
     * @param id
     * @return
     */
    
    @Override
    public List<Long> getMySubscribe(Long id){
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_SUBSCRIBE,id);
        Set<Object> myFollowers = redisUtil.sGet(fanUsersKey);
        List<Long> user = new ArrayList<>();
        for (Object userId:myFollowers){
            user.add(Long.valueOf(String.valueOf(userId)));
        }
        return user;
    }

    /**
     * 获取爷的粉丝
     * @param user
     * @return
     */
    @Override
    public List<Long> getMyFans(User user){
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_FANS,user.getUserId());
        Set<Object> myFans = redisUtil.sGet(fanUsersKey);
        log.info(String.valueOf(myFans));
        List<Long> users = new ArrayList<>();
        for (Object userId:myFans){
            users.add(Long.valueOf(String.valueOf(userId)));
        }
        return users;
    }

    /**
     * 获取爷关注人的数量
     * @param user
     * @return
     */
    @Override
    public Long myFollowCount(User user){
        log.info("执行了查找关注数");
        redisUtil = getRedisUtil();
       String fanUsersKey = String.format(MY_SUBSCRIBE,user.getUserId());
        return redisUtil.sGetSetSize(fanUsersKey);

    }

    /**
     * 获取关注爷的人的数量
     * @param user
     * @return
     */

    @Override
    public Long followMeCount(User user){
        log.info("执行了查找关注数");
        redisUtil = getRedisUtil();
        String fanUsersKey = String.format(MY_FANS,user.getUserId());
        return redisUtil.sGetSetSize(fanUsersKey);
    }



}
